package com.bw.controller;

import com.bw.entity.*;
import com.bw.mapper.StudentMapper;
import com.bw.service.StudentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.spring.kun.common.TokenUtils;
import org.spring.kun.common.UploadPicUtils;
import org.spring.kun.common.ZzyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@Controller
public class ListController {

        @Autowired
        StudentMapper studentMapper;
        @Autowired
        StudentService studentService;
    @RequestMapping("/list.do")
    public String list(StudentQuery query, Integer pageNum,HttpServletRequest request, Model model){

        // 获取当前登录用户
        User userFromRequest = TokenUtils.getLoginUserFromRequest(request, User.class);
        model.addAttribute("nickName",userFromRequest.getNickName());
        model.addAttribute("avatarUrl",userFromRequest.getAvatarUrl());

        // 分页查询
        PageHelper.startPage(pageNum == null ? 1:pageNum,3);
        List<StudentRoom> list = studentMapper.list(query);

        PageInfo<StudentRoom> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo",pageInfo);
        return "list";
    }

    @RequestMapping("/addPage.do")
    public String addPage(Model model){
        // 添加 页面的下拉框数据
       List<Dict> apartmentList = studentMapper.apartmentList();
        model.addAttribute("apartmentList",apartmentList);
        List<Dict> roomList = studentMapper.roomList();
        model.addAttribute("roomList",roomList);
        List<Dict> studentList = studentMapper.studentList();
        model.addAttribute("studentList",studentList);
        return "add";
    }

    @RequestMapping("/add.do")
    public String add(@Valid StudentRoomAdd studentRoomAdd, BindingResult result,Model model,HttpServletRequest request){
        if (result.hasErrors()){

            model.addAttribute("error",result.getAllErrors().get(0).getDefaultMessage());
            // 添加 页面的下拉框数据
            List<Dict> apartmentList = studentMapper.apartmentList();
            model.addAttribute("apartmentList",apartmentList);
            List<Dict> roomList = studentMapper.roomList();
            model.addAttribute("roomList",roomList);
            List<Dict> studentList = studentMapper.studentList();
            model.addAttribute("studentList",studentList);
            return "add";
        }
        studentService.add(studentRoomAdd);
        boolean sendSms = ZzyUtils.sendSms("17343092621", "入住成功", 5);

        String upload = UploadPicUtils.upload(studentRoomAdd.getPicFile(), request);
        System.out.println("上传图片成功，图片的访问路径是 " + upload);
        return "redirect:list.do";
    }

    public static void main(String[] args) {
        boolean sendSms = ZzyUtils.sendSms("17343092621", "入住成功", 5);

    }
}
